package com.fly.control;

import java.sql.ResultSet;

import com.fly.conn.DBConnect;
import com.fly.model.Unit;

public class UnitControl {
	public static boolean insertUnit(Unit c){
		DBConnect dbc = new DBConnect();
		String sql = "insert into paramunit(name) values (?)";
		dbc.prepareStatement(sql);
		try{
			dbc.setString(1, c.getName());
			dbc.executeUpdate();
			return true;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("insertUnit ERROR");
		} finally{
			dbc.close();
		}
		return false;
	}
	public static boolean deleteUnit(int id){
		String sql = "delete * from paramunit where id =" + id;
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		try{
			dbc.executeUpdate();
			return true;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("deleteUnit ERROR");
		} finally{
			dbc.close();
		}
		return false;
	}
	public static boolean updateUnit(int id, Unit c){
		String sql = "update paramunit set name = ? where id = " + id;
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		try{
			dbc.setString(1, c.getName());
			dbc.executeUpdate();
			return true;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("updateUnit ERROR");
		} finally{
			dbc.close();
		} 
		return false;
	}
	public static Unit getUnit(int id){
		String sql = "select * from paramunit where id =" + id;
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		Unit c = new Unit();
		try{
			rs = dbc.executeQuery();
			while(rs.next()){
				c.setName(rs.getString("name"));
			}
			return c;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("getUnit ERROR");
		} finally{
			dbc.close();
		}
		return null;
	}
}
